<?php
/**
 * Created by PhpStorm.
 * User: gyq
 * Date: 2018/11/4
 * Time: 12:04
 */

namespace app\api\controller\v1;


use app\api\validate\Count;

use app\api\model\Product as ProductModel;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\ProductException;

class Product
{

//    url /product/recent
    public function getRecent($count = 15)
    {
        (new Count())->goCheck();
        $result = ProductModel::getRecentProduct($count);
        if ($result->isEmpty()) {
            throw new ProductException();
        }
        $result = $result->hidden(['summary']);
        return $result;
    }

//    url /product/allInCategory
    public function getAllInCategory($id)
    {
        (new IDMustBePositiveInt())->goCheck();
        $products = ProductModel::getProductByCategoryId($id);
        if ($products->isEmpty()) {
            throw new ProductException();
        }
        $products = $products->hidden(['summary']);
        return $products;
    }

    public function getProductDetail($id)
    {
        (new IDMustBePositiveInt())->goCheck();
        $products = ProductModel::getProductDetail($id);
        if(!$products){
            throw new ProductException();
        }
        return $products;
    }
}